package com.kly.cashmall.widget.datepicker;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;

import androidx.fragment.app.FragmentActivity;

public class DatePickerDialogUtil {

  public static final String FRAGMENT_TAG = "DatePickerAndroid";

  /* package */ static final String ARG_DATE = "date";
  /* package */ static final String ARG_MINDATE = "minDate";
  /* package */ static final String ARG_MAXDATE = "maxDate";
  /* package */ static final String ARG_MODE = "mode";

  /* package */ static final String ACTION_DATE_SET = "dateSetAction";
  /* package */ static final String ACTION_DISMISSED = "dismissedAction";

  public interface DatePickerDialogListener extends DatePickerDialog.OnDateSetListener,
      DialogInterface.OnDismissListener {

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day);

    @Override
    public void onDismiss(DialogInterface dialog);
  }

  public static void openDatePickerDialog(Activity activity, Options options,
      DatePickerDialogListener listener) {
    // We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
    // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
    if (activity instanceof FragmentActivity) {
      androidx.fragment.app.FragmentManager fragmentManager =
          ((FragmentActivity) activity).getSupportFragmentManager();
      androidx.fragment.app.DialogFragment oldFragment =
          (androidx.fragment.app.DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
      if (oldFragment != null) {
        oldFragment.dismiss();
      }
      SupportDatePickerDialogFragment fragment = new SupportDatePickerDialogFragment();
      fragment.setArguments(Options.createFragmentArguments(options));
      fragment.setOnDismissListener(listener);
      fragment.setOnDateSetListener(listener);
      fragment.show(fragmentManager, FRAGMENT_TAG);
    } else {
      FragmentManager fragmentManager = activity.getFragmentManager();
      DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
      if (oldFragment != null) {
        oldFragment.dismiss();
      }
      DatePickerDialogFragment fragment = new DatePickerDialogFragment();
      fragment.setArguments(Options.createFragmentArguments(options));
      fragment.setOnDismissListener(listener);
      fragment.setOnDateSetListener(listener);
      fragment.show(fragmentManager, FRAGMENT_TAG);
    }
  }

  public static class Options {
    private long date;
    private long minDate;
    private long maxDate;
    private DatePickerMode mode = DatePickerMode.DEFAULT;

    public Options(){}

    public Options setDate(long date) {
      this.date = date;
      return this;
    }

    public Options setMinDate(long minDate) {
      this.minDate = minDate;
      return this;
    }

    public Options setMaxDate(long maxDate) {
      this.maxDate = maxDate;
      return this;
    }

    public Options setMode(DatePickerMode mode) {
      this.mode = mode;
      return this;
    }

    static Bundle createFragmentArguments(Options options) {
      Bundle args = new Bundle();
      if(options == null){
        return args;
      }

      if(options.date > 0){
        args.putLong(ARG_DATE, options.date);
      }

      if(options.minDate > 0){
        args.putLong(ARG_MINDATE, options.minDate);
      }

      if(options.maxDate > 0){
        args.putLong(ARG_MAXDATE, options.maxDate);
      }

      if(options.mode != null && !DatePickerMode.DEFAULT.equals(options.mode)){
        args.putString(ARG_MODE, options.mode.name());
      }

      return args;
    }
  }
}
